package com.songlanyun.modules.donationpoints.controller.api;

import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.donationpoints.entity.DonationPointsEntity;
import com.songlanyun.modules.donationpoints.service.DonationPointsService;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 公益积分捐赠
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-02 15:33:13
 */
@RestController
@RequestMapping("/api/donation/points")
public class DonationPointsApiController extends AbstractApiController {
    @Autowired
    private DonationPointsService donationPointsService;

    @Resource
    private PublicBenefitMsgService publicBenefitMsgService;

    /**
     * 列表
     */
    @GetMapping("/page")
    //@RequiresPermissions(":donationrecord:list")
    public R page(@RequestParam Map<String, Object> params) {
        params.put("donorId", getUserId(true));
        PageUtils page = donationPointsService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        DonationPointsEntity donationRecord = donationPointsService.getById(id);

        return R.ok().put("data", donationRecord);
    }

    /**
     * 捐赠
     */
    @PostMapping("/donate")
    public R donate(@RequestBody DonationPointsEntity donationRecord) {
        ValidatorUtils.validateEntity(donationRecord);
        donationRecord.setDonorId(getUserId(true));
        DonationPointsEntity donationPoints = donationPointsService.donate(donationRecord);

        //发送公益消息
        publicBenefitMsgService.sendMsg(
                PublicBenefitMsgTO.builder()
                        .type(DonationConstant.MsgType.DONATE)
                        .modelType(DonationConstant.DonateMsgType.CHARITABLE_DONATIONS.getCode())
                        .modelId(donationPoints.getId()).build()
        );
        return R.ok();
    }

}
